<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 具体页面结构 -->
        <el-row type="flex" justify="end" style="margin-bottom:15px">
          <el-button type="primary" size="small" @click="hAdd(1,'0')">添加权限</el-button>
        </el-row>
        <el-table
          :data="perList"
          style="width: 100%;margin-bottom: 20px;"
          row-key="id"
          border
          default-expand-all
          :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
        >
          <el-table-column
            prop="name"
            label="名称"
            sortable
            width="180"
          />
          <el-table-column
            prop="code"
            label="标识"
          />
          <el-table-column
            prop="description"
            label="描述"
          />
          <el-table-column
            fixed="right"
            label="操作"
            width="140"
          >
            <template slot-scope="{row}">
              <el-button :disabled="row.type !==1" type="text" size="small" @click="hAdd(2,row.id)">添加</el-button>
              <el-button type="text" size="small" @click="hEdit(row.id)">编辑</el-button>
              <el-button type="text" size="small" @click="del(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-dialog :visible.sync="showDialog" title="弹层标题" @close="close">
          <!-- 表单内容 -->
          <el-form label-width="100px">
            <el-form-item label="权限名称">
              <el-input v-model="formData.name" />
            </el-form-item>
            <el-form-item label="权限标识">
              <el-input v-model="formData.code" />
            </el-form-item>
            <el-form-item label="权限描述">
              <el-input v-model="formData.description" />
            </el-form-item>
            <el-form-item label="权限启用">
              <el-switch
                v-model="formData.enVisible"
                active-text="启用"
                active-value="1"
                inactive-text="不启用"
                inactive-value="0"
              />
            </el-form-item>
          </el-form>

          <template #footer>
            <div style="text-align: right;">
              <el-button @click="showDialog = false">取消</el-button>
              <el-button type="primary" @click="sub">确定</el-button>
            </div>
          </template>
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>
<script>
import { getpermission, addPermission, getPermissionById, updatePermission, removePermission } from '@/api/permission'
import { tranListToTreeData } from '@/utils/tree'
export default {
  data() {
    return {
      perList: [],
      showDialog: false, // 是否显示弹层
      formData: {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: '' // 类型
      },
      isEdit: false
    }
  },
  created() {
    this.initperList()
  },
  methods: {
    async initperList() {
      try {
        const res = await getpermission()
        this.perList = tranListToTreeData(res.data)
      } catch (err) {
        console.log(err)
      }
    },
    async hAdd(type, pid) {
      this.showDialog = true
      this.formData.type = type
      this.formData.pid = pid
    },
    async hEdit(id) {
      this.showDialog = true
      this.isEdit = true
      try {
        const res = await getPermissionById(id)
        this.formData = res.data
      } catch (err) {
        console.log(err)
      }
    },
    async  sub() {
      if (this.isEdit) {
        try {
          const res = await updatePermission(this.formData)
          console.log('update', res)
          this.$message.success('更新权限成功')
        } catch (err) {
          console.log(err)
        }
      } else {
        try {
          const res = await addPermission(this.formData)
          console.log(res)
          this.$message.success('添加权限成功')
        } catch (err) {
          console.log(err)
        }
      }
      this.initperList()
      this.showDialog = false
    },
    close() {
      this.isEdit = false
      this.formData = {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: '' // 类型
      }
    },
    del(id) {
      this.$confirm('亲，是否确定删除该权限?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await removePermission(id)
        this.initperList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
